package com.tianyu.model.ro;

import com.tianyu.model.enums.Ro2State;
import lombok.Data;
import lombok.experimental.Accessors;


/**********************************
 * @author swyan
 * @description 接口返回通用类型 Ro(Return Object)(请求返回类型)
 * @date 2024/7/15 - 14:16
 **********************************/


@Data
@Accessors(chain = true)
public class Ro2<T> {
    private Integer code;
    private String msg;
    private T data;

    public Ro2() {
        setResultEnum(Ro2State.SUCCESS);
    }

    public Ro2(Ro2State re) {
        this.code = re.getValue();
        this.msg = re.getMessage();
    }

    public Ro2<T> setResultEnum(Ro2State re) {
        this.code = re.getValue();
        this.msg = re.getMessage();
        return this;
    }

    public Ro2<T> failureResult(String message) {
        this.code = Ro2State.FAILED.getValue();
        this.msg = message;
        return this;
    }

    public Ro2<T> failureResult() {
        this.code = Ro2State.FAILED.getValue();
        this.msg = Ro2State.FAILED.getMessage();
        return this;
    }

    public static Ro2<?> success(){
        return new Ro2<>();
    }

    public static Ro2<?> success(Object data){
        return new Ro2<>().setData(data);
    }

    public static Ro2<?> error(){
        return new Ro2<>().failureResult();
    }

    public static Ro2<?> error(String message){
        return new Ro2<>().failureResult(message);
    }
}
